import { HostSettingsServiceProxy, ProjectConfigEditDto } from '../service-proxies';
import { apiHttpClient } from '../utils';
import { AppConsts } from '/@/abpPro/AppConsts';
import { PROJ_CFG_KEY } from '/@/enums/cacheEnum';
import { Persistent } from '/@/utils/cache/persistent';

class SettingService {
  private _hostSettingService: HostSettingsServiceProxy;

  get hostSettingService(): HostSettingsServiceProxy {
    if (!this._hostSettingService) {
      this._hostSettingService = new HostSettingsServiceProxy(
        AppConsts.remoteServiceBaseUrl,
        apiHttpClient,
      );
    }
    return this._hostSettingService;
  }

  /**
   * 更新配置
   */
  public updateProjectConfig() {
    const cache = Persistent.getLocal(PROJ_CFG_KEY);
    const input = new ProjectConfigEditDto();
    input.projectConfig = JSON.stringify(cache);
    this.hostSettingService.updateProjectConfig(input).then(() => {});
  }

  /**
   * 获取配置
   * @returns ProjectConfig
   */
  async getProjectConfig() {
    return await this.hostSettingService.getProjectConfig();
  }
}

const settingService = new SettingService();

export { settingService };
